package com.genius.controller;

import cn.hutool.json.JSONUtil;
import com.genius.annotation.PreFuncAuth;
import com.genius.annotation.PreInnerAuth;
import com.genius.core.JSONResult;
import com.genius.monitor.bo.DeleteOperationLogByIdsBO;
import com.genius.monitor.bo.SelectOperationLogByPageBO;
import com.genius.monitor.pojo.OperationLog;
import com.genius.service.OperationLogService;
import com.genius.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("operationLog")
@Api(value = "操作日志接口", tags = {"用于操作日志的接口"})
public class OperationLogController {

    @Autowired
    private OperationLogService operationLogService;

    @PostMapping("/selectOperationLogByPage")
    @ApiOperation(value = "查询操作日志",notes = "查询操作日志", httpMethod = "POST")
    @PreFuncAuth("MONITOR:OPERATIONLOG:SELECT")
    public JSONResult selectOperationLogByPage(@Valid @RequestBody SelectOperationLogByPageBO selectOperationLogByPageBO){
        int pageNum = selectOperationLogByPageBO.getPageNum();
        int pageSize = selectOperationLogByPageBO.getPageSize();
        OperationLog operationLog = JSONUtil.parse(selectOperationLogByPageBO).toBean(OperationLog.class);
        PageUtils.startPage(pageNum,pageSize);
        List<OperationLog> operationLogList = operationLogService.selectOperationLogByPage(operationLog);
        Map<String,Object> result = PageUtils.getResultTableData(operationLogList);
        return JSONResult.ok(result);
    }

    @PostMapping("/insertOperationLog")
    @PreInnerAuth
    public JSONResult insertOperationLog(@Valid @RequestBody OperationLog operationLog) {
        int rows = operationLogService.insertOperationLog(operationLog);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/deleteOperationLogByIds")
    @ApiOperation(value = "删除操作日志",notes = "删除操作日志", httpMethod = "POST")
    @PreFuncAuth("MONITOR:OPERATIONLOG:DELETE")
    public JSONResult deleteOperationLogByIds(@Valid @RequestBody DeleteOperationLogByIdsBO deleteOperationLogByIdsBO) {
        int rows = operationLogService.deleteOperationLogByIds(deleteOperationLogByIdsBO.getOperationLogIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
